<?php
class AppModel extends Model
{
	private $sql_function = array("CURDATE()","NOW()");
	public $sanitize;


    public function myGenerateList($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null, $separator = '')
    {
        if ($keyPath == null && $valuePath == null && $this->hasField($this->displayField)) {
          $fields = array($this->primaryKey, $this->displayField);
        } else {
          $fields = null;
        }
        $recursive = $this->recursive;
        $result = $this->findAll($conditions, $fields, $order, $limit);
        $this->recursive = $recursive;

        if(!$result) {
          return false;
        }

        if ($keyPath == null) {
          $keyPath = '{n}.' . $this->name . '.' . $this->primaryKey;
        }

        if ($valuePath == null) {
          $valuePath = '{n}.' . $this->name . '.' . $this->displayField;
        }

        // extract the keys as normal
        $keys = Set::extract($result, $keyPath);


        $finalVals = array();

        // explode the value path by our delimiter
        $tmpVals = explode('#', $valuePath);

        $i = 0;
        foreach($tmpVals as $tmpVal) {
          // extract the data for each path sibling
          $vals = Set::extract($result, $tmpVal);

          // and insert it at the appropriate position in our final value array
          // use the separator when we need to append the values
          foreach($vals as $key => $value) {
            if(!array_key_exists($key, $finalVals)) {
              $finalVals[$key] = $value;
            } else {
              $finalVals[$key] .= $separator.$value;
            }
          }
          $i++;
        }


        if (!empty($keys) && !empty($finalVals)) {
          $out = array();
          $return = array_combine($keys, $finalVals);
          return $return;
        }
        return null;
    }


    /**
     * @param integer $id
     * @param array $value
     *
     * @return $id if successfully and null if error
     */
    public function _save($id = 0, $value = array())
    {
        $this->id = $id;
        if (is_array($value)) {
            $rs = $this->save($value, array('validate' => true));
            if ($rs) {
                 if ($this->id > 0) {
                     return $this->id;
                 } else {
                     return array('id' => $this->{$this->name}->getInsertID());
                 }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * @desc execute custom insert statement
     *
     * @param $data
     * @return unknown_type
     */
    public function __insert($data = array())
    {
    	if (!is_array($date)) return null;

		// initialize with open parens
		$ftxt = $vtxt = "(";

		// Iterate over the values hash
		$first = true;

		foreach ($values as $key => $val) {
			if ($first) {
				$first = false;
			} else {
				$ftxt .= ", ";
				$vtxt .= ", ";
			}
			$ftxt .= "`" . mysql_escape_string($key) . "`";
			// SQL functions are not quoted or escaped...
			if (array_search($val, $this->sql_function)) {
				$vtxt .= $val;
			} else {
				$vtxt .= "'" . mysql_escape_string($val) . "'";
			}
		}
		$ftxt .= ")";
		$vtxt .= ")";

		$query = "INSERT INTO `" . mysql_escape_string($table) . "` " . $ftxt
		. " VALUES " . $vtxt;

		self::__query($query);
		$id = self::__query("SELECT LAST_INSERT_ID();", true);
		return $id[0][0];
    }

	/**
	 * Do an arbitrary database query.  $query is the SQL query to
	 * be executed.  It will return the result of the query as is, or
	 * the result set as an array if $fetcharray is true.
	 */
	public function __query($query)
	{
		return self::query($query);
	}
}
?>